<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * 采集 脚本
 */
class Cron_Snoopy extends CI_Controller {
	private $logFile;
	private $db;
	public function __construct(){  
        parent::__construct();
        if(!is_cli()){
        	header("HTTP/1.0 404 Not Found");
        	show_error('Not Found',404);
        	die;
        }
    }

    /**
     * 分类{Type}：
     * 综合性新闻
     * 商品-期货新闻
     * 股票市场新闻
     * 经济
     * economy-news
     * 
     * 抓取http://cn.investing.com/news/{Type}/{Page} 内容
     *
     * 截取列表页正文：
     * (?s)<!\-\- Page Content \-\->(.+?)<!\-\- /Page Content \-\->
     * 
     * 获取列表项：
     * (?s)<article class="articleItem">\s+<a href=".+?\-(\d+)" class="img"><img src="([^"]+)".+?</article>
     * 
     * Id = result[1]
     * ====================
     * 
     * 新闻内页：
     * http://cn.investing.com/news/news-item-{Id}

     * 标题-时间：
     * <h1 itemprop="headline" class="articleHeader">(.+?)</h1>\s+<div class="contentSectionDetails">\s+<i><img src="([^"]+)"></i>.+?<span>([^<]+)</span>\s+</div>
     * Time = result[3]
     * Sscanf(Time, "%d 分钟以前", &mins)
     * Sscanf(Time, "%d 小时以前", &hours)
     * Sscanf(Time, "%d年%02d月%02d日 %02d:%02d", &year, &month, &day, &hour, &min)

     * 详细内容：
     * (?s)<div class="arial_14 clear WYSIWYG newsPage">\s+<div id="imgCarousel" class="imgCarousel">.+?</div>(.+?)<div id="floatingH1" class="tradeNowFloat instrumentFloater WYSIWYGFloater js-floated-header-container">
     * Content = result[1]
     * 
     * @return [type] [description]
     */
	public function investing(){
		set_time_limit(120);
		$tyles = array(1=>'综合性新闻',2=>'商品-期货新闻',3=>'股票市场新闻',4=>'经济',5=>'economy-news');
		// $tyles = array('综合性新闻');
		$this->logFile = "Cron_Snoopy_investing";
		$this->load->library('Snoopy');
	 	$this->db = $this->load->database('default',true);
		$table = 'news';
		$this->db->where('source','investing');
		$this->db->select_max('articleId');
		$query = $this->db->get($table);
		$max = 0;
		if($query->row()){
			$data = $query->row_array();
			$max = $data['articleId'];
		}
		foreach($tyles as $typeId=>$type){

			$url = "http://cn.investing.com/news/".$type;
			$this->snoopy->fetch($url);
			$html = $this->snoopy->results;
			preg_match_all('/(?s)<\!\-\- Page Content \-\->(.+?)<\!\-\- \/Page Content \-\->/', $html, $arr);
			$articleList = $arr[1];
			preg_match_all('/(?s)<article class="articleItem">\s+<a href=".+?\-(\d+)" class="img">.+?<\/article>/', $arr[1][0], $arr);
			$articleIds = $arr[1];
			if(!empty($articleIds)) foreach($articleIds as $articleId){
				if($articleId<=$max){
					echo "$articleId no\n";
					continue;
				} 
				echo "$articleId add\n";
				$innerUrl = "http://cn.investing.com/news/news-item-".$articleId;
				$this->snoopy->fetch($innerUrl);
				$innerHtml = $this->snoopy->results;
				//标题-时间
				preg_match_all('/<h1 itemprop="headline" class="articleHeader">(.+?)<\/h1>\s+<div class="contentSectionDetails">\s+<i><img src="([^"]+)"><\/i>.+?<span>([^<]+)<\/span>\s+<\/div>/', $innerHtml, $result);
				preg_match_all('/(?s)<div class="arial_14 clear WYSIWYG newsPage">\s+<div id="imgCarousel" class="imgCarousel">.+?<\/div>(.+?)<div id="floatingH1" class="tradeNowFloat instrumentFloater WYSIWYGFloater js-floated-header-container">/', $innerHtml, $result2);
				$title = $result[1][0];
				$time = $result[3][0];
				$flag = true;
				if(strpos($time,'小时')!==false){
					$int = intval($time);
					$datetime = date('Y-m-d H:i:s',strtotime("-".$int." hours"));
				}else if(strpos($time,'分钟')!==false){
					$int = intval($time);
					$datetime = date('Y-m-d H:i:s',strtotime("-".$int." minutes"));
				}else{
					$flag = false;
				}
				$content = preg_replace("#<a[^>]*>(.*?)</a>#is", "$1", $result2[1][0]);
				if($flag){
					$where = array('source'=>'investing','articleId'=>$articleId);
					$query = $this->db->select()->from($table)->where($where)->limit(1)->get();
					if (!$query->row()) {
						$inArr = array(
							'type' => $typeId,
							'source' => 'investing',
							'articleId' => $articleId,
							'title' => $title,
							'content' => $content,
							'create_time' => $datetime,
							'add_time' => strtotime($datetime)
						);
						$this->db->insert($table,$inArr);
					}
				}
			}
		}
		
		
	}
		
}
